package com.youdianstar.app.common.util.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import com.youdianstar.app.common.R


/**
 * File: DialogHelper
 * Author: 82149 Create: 2021/9/22 17:37
 * Changes (from 2021/9/22)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class DialogHelper (onCancelListener : OnDialogCancelListener,mStyle:Int? = null) : DialogInterface.OnCancelListener{

    interface OnDialogCancelListener {
        /**
         * 取消按钮点击的回调
         *
         * @param dialog 弹窗
         */
        fun onCancelListener(dialog: DialogInterface?)
    }

    /**
     * 弹窗的样式
     */
    private var mStyle :Int? = null

    /**
     * 监听Cancel
     */
    private var cancelListener: OnDialogCancelListener? = null

    init {
        this.mStyle = mStyle
        this.cancelListener = onCancelListener
    }

    override fun onCancel(dialog: DialogInterface?) {
        cancelListener?.onCancelListener(dialog)
    }


    /**
     * 调整dialog的size和位置
     *
     * @param dialog  dialog
     * @param x
     * @param y
     * @param gravity 位置
     */
    fun adjustSizeGravity(dialog: Dialog?, x: Int, y: Int, gravity: Int) {
        dialog?.window?.let {
            val params = it.attributes
            params?.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND
            it.attributes?.apply {
                height = WindowManager.LayoutParams.WRAP_CONTENT
                if (x != 0) width = x
                if (y != 0) height = y
                if (gravity != 0) it.setGravity(gravity)
                it.setLayout(width, height)
            }
        }
    }

    /**
     * 创建和显示弹窗 自定义布局
     *
     * @param context    上下文
     * @param mStyle     样式
     * @param cancelable 取消
     */
    fun createBaseDialog(context: Context, mStyle: Int, cancelable: Boolean, mContentView: View): Dialog {
        return Dialog(context, mStyle).apply {
            setCancelable(cancelable)
            setCanceledOnTouchOutside(cancelable)
            setContentView(mContentView)
        }
    }

    /**
     * 创建和显示弹窗 自定义布局
     *
     * @param context    上下文
     * @param mStyle     样式
     * @param cancelable 取消
     */
    fun createViewAlertDialog(context: Context, mStyle: Int, cancelable: Boolean, mContentView: View?): AlertDialog {
        return AlertDialog.Builder(context, mStyle)
            .setView(mContentView)
            .setCancelable(cancelable)
            .setOnCancelListener(this)
            .create()
    }

    /**
     * 可设置标题、内容、按钮文字，监听的对话框
     *
     * @param context
     * @param title
     * @param message
     * @param okString
     * @param cancelString
     * @param onOkClickListener
     * @param onCancelClickListener
     * @return
     */
    private fun createAlertDialog(context: Context,
                                  title: String, message: String, okString: String, cancelString: String,
                                  onOkClickListener: DialogInterface.OnClickListener,
                                  onCancelClickListener: DialogInterface.OnClickListener): AlertDialog {
        val builder = AlertDialog.Builder(context).setCancelable(true)
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title)
        }
        if (!TextUtils.isEmpty(message)) {
            builder.setMessage(message)
        }
        if (!TextUtils.isEmpty(cancelString)) {
            builder.setNegativeButton(cancelString, onCancelClickListener)
        }
        if (!TextUtils.isEmpty(okString)) {
            builder.setPositiveButton(okString, onOkClickListener)
        }
        return builder.create()
    }


    /**创建加载框*/
    fun getLoadingDialog(context: Context,cancelable:Boolean):AlertDialog{
        val mDialogView = LayoutInflater.from(context).inflate(R.layout.progress_loading, null)
        return createViewAlertDialog(context, R.style.AlertDialogStyle, cancelable, mDialogView);
    }

}